import getShareId, { requestShareId } from './shareId'

// 在onshareappmessage生命周期钩子中调用
// 如果新增加的页面，增加新的id即可
// ID规则 xx|xx
// 前面一或两位表示页面，只有在新增页面需要分享时才添加
// 后两位表示同一个页面不同分享路径
// 为了方便以后添加参数
// 务必保证每个分享都是唯一的
// 例如同一个页面 可以分享当前页面或页面具体内容 那么算做两个分享
// 需要添加两个id xx01 xx02

const shareMap = [
  { id: 101, path: '/pages/tabbar/card/card', params: { originId: '2001' } }, // 名片列表页分享名片
  { id: 201, path: '/pages/tabbar/card/card', params: { originId: '2002' } }, // 名片页分享名片
  { id: 203, path: '/subs/dynamic/pages/dynamic-detail/dynamic-detail', params: { originId: '2011' } }, // 名片页分享普通动态
  { id: 204, path: '/subs/dynamic/pages/dynamic-detail/dynamic-detail', params: { originId: '2011' } }, // 名片页分享普通动态
  { id: 205, path: '/subs/dynamic/pages/dynamic-detail/dynamic-detail', params: { originId: '2011' } }, // 名片页分享普通动态
  { id: 204, path: '/subs/website/pages/web-view/web-view', params: { originId: '2011' } }, // 名片页分享了链接动态
  { id: 301, path: '/pages/tabbar/mall/mall', params: { originId: '2007' } }, // 商城首页
  { id: 401, path: '/pages/tabbar/website/website', params: { originId: '2014' } }, // 官网首页
  { id: 501, path: '/pages/tabbar/dynamic/dynamic', params: { originId: '2013' } },
  { id: 502, path: '/subs/dynamic/pages/dynamic-detail/dynamic-detail', params: { originId: '2011' } },
  { id: 503, path: '/subs/website/pages/web-view/web-view', params: { originId: '2011' } },
  { id: 504, path: '/subs/dynamic/pages/dynamic-detail/dynamic-detail', params: { originId: '2011' } },
  { id: 505, path: '/subs/dynamic/pages/dynamic-detail/dynamic-detail', params: { originId: '2011' } },
  { id: 506, path: '/subs/dynamic/pages/dynamic-detail/dynamic-detail', params: { originId: '2012' } },
  { id: 507, path: '/subs/website/pages/web-view/web-view', params: { originId: '2012' } },
  { id: 601, path: '/subs/website/pages/web-view/web-view', params: { originId: '2017' } }, // webview 招聘
  { id: 602, path: '/subs/website/pages/web-view/web-view', params: { originId: '2012' } }, // webview 动态
  { id: 603, path: '/subs/website/pages/web-view/web-view', params: { originId: '2015' } }, // webview 资讯
  { id: 604, path: '/subs/website/pages/web-view/web-view', params: {} }, // webview 案例
  { id: 605, path: '/subs/website/pages/web-view/web-view', params: {} }, // webview 其他情景
  { id: 701, path: '/pages/tabbar/card/card', params: { originId: '2003' } }, // 个人中心分享名片
  { id: 702, path: '/subs/briefcase/pages/preview/preview', params: { originId: '2005' } }, // 个人中心分享文件
  { id: 801, path: '/subs/mall/pages/manual-horizontal/manual-horizontal', params: { originId: '2004' } }, // 公文包手册列表页横屏页
  { id: 802, path: '/subs/mall/pages/manual-vertical/manual-vertical', params: { originId: '2004' } }, // 公文包手册列表页竖屏页
  { id: 901, path: '/subs/mall/pages/manual-horizontal/manual-horizontal', params: { originId: '2004' } }, // 手册横屏页
  { id: 1001, path: '/subs/mall/pages/manual-vertical/manual-vertical', params: { originId: '2004' } }, // 手册竖屏页
  { id: 1101, path: '/subs/briefcase/pages/preview/preview', params: { originId: '2005' } }, // 公文包列表页
  { id: 1201, path: '/subs/briefcase/pages/preview/preview', params: { originId: '2005' } }, // 公文包预览页
  { id: 1301, path: '/subs/briefcase/pages/preview/preview', params: { originId: '2005' } }, // 公文包搜索页
  { id: 2101, path: '/subs/activity/pages/activity-form-detail/activity-form-detail', params: { originId: '2006' } }, //
  { id: 2201, path: '/subs/activity/pages/activity-form-detail/activity-form-detail', params: { originId: '2006' } }, //
  { id: 2202, path: '/subs/interactive/pages/activity-detail/activity-detail', params: { } }, //
  { id: 2203, path: '/subs/interactive/pages/activity-detail/activity-detail', params: { } }, //
  { id: 2301, path: '/subs/interactive/pages/say-detail/say-detail', params: { } },
  { id: 2302, path: '/subs/interactive/pages/say/say', params: { } },

  // 内部跳转 99xx
  { id: 9901, path: '/pages/tabbar/card/card', params: { sourceType: 6, originId: 4010 } },
  { id: 9902, path: '/pages/tabbar/card/card', params: { sourceType: 14, originId: 4001 } },
  { id: 9903, path: '/pages/tabbar/card/card', params: { sourceType: 14, originId: 4013 } },
  { id: 9904, path: '/pages/tabbar/card/card', params: { sourceType: 7, originId: 4002 } },
  { id: 9905, path: '/pages/tabbar/card/card', params: { sourceType: 7, originId: 4003 } },
  { id: 9906, path: '/pages/tabbar/card/card', params: { sourceType: 14, originId: 4001 } },
  { id: 9907, path: '/pages/tabbar/card/card', params: { sourceType: 7, originId: 4002 } },
]

const pathMap = { }
shareMap.forEach(({ id, ...item }) => {
  pathMap[id] = item
})

// 异步获取分享地址 用于自动跳转
export function getShareUrlAsync(id, params, shareIdParams) {
  return new Promise((resolve, reject) => {
    requestShareId(null, shareIdParams)
      .then(routeId => resolve(getShareUrl(id, { ...params, routeId })))
      .catch(error => reject(error))
  })
}

// 同步获取分享地址 用于分享按钮
export function getShareUrl(id, { routeId, ...params } = {}) {
  const item = pathMap[id]

  if (!item) throw new Error(`不存在的分享Id${id}`)

  const queryParams = {
    routeId: routeId || getShareId(),
    sourceType: 1,
    ...item.params,
    ...params
  }

  const path = `${item.path}?${serialize(queryParams)}`
  console.log('当前页分享路径及参数：', path)
  return path
}

const serialize = function(obj) {
  const kvs = Object.keys(obj).map(key => {
    const value = obj[key]
    if (value === null || value === undefined) {
      throw new Error(`存在无效的属性${key}`)
    }

    // 存在非法字符才对uri编码 避免直接编码出现二次编码的情况
    const encodeValue = /^[a-zA-Z0-9%.\-_~!'()*]+$/i.test(value) ? value : encodeURIComponent(value)
    return `${key}=${encodeValue}`
  })
  return kvs.join('&')
}
